//
//  ScrollView.m
//  WXMovie
//
//  Created by apple on 16/4/1.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "ScrollView.h"

@implementation ScrollView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self _loadData];
        [self _createView];
        _count = 0;
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self _loadData];
        [self _createView];
        _count = 0;
    }
    return self;
    
}
//数据加载
- (void)_loadData {
    
    _dataArray = [NSMutableArray array];
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"news_list.json" ofType:nil]] options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary *dic in array) {
        NewsModel *model = [[NewsModel alloc] init];
        
        model.title = dic[@"title"];
        model.image = dic[@"image"];

        [_dataArray addObject:model];
    }
}



//滑动视图的创建
-(void)_createView {
    

    NewsModel *model1 = _dataArray[_dataArray.count - 1];
    NewsModel *model2 = _dataArray[0];
    NewsModel *model3 = _dataArray[1];
    
//    NSLog(@"%@", model1);
    
    _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];

    _scroll.contentSize = CGSizeMake(_scroll.width * 3, 0);
    _scroll.scrollEnabled = YES;
    _scroll.pagingEnabled = YES;
    _scroll.showsHorizontalScrollIndicator = YES;
    _scroll.backgroundColor = [UIColor clearColor];
    _scroll.contentOffset = CGPointMake(_scroll.width, 0);
    _scroll.delegate = self;
    [self addSubview:_scroll];

    _imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _scroll.width, 150)];
    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:model1.image]];
    [_scroll addSubview:_imageView1];
    _imageView2= [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.width, 0, _scroll.width, 150)];
    [_imageView2 sd_setImageWithURL:[NSURL URLWithString:model2.image]];
    [_scroll addSubview:_imageView2];
    _imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.width * 2, 0, _scroll.width, 150)];
    [_imageView3 sd_setImageWithURL:[NSURL URLWithString:model3.image]];
    
    [_scroll addSubview:_imageView3];

    _titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, _imageView1.height - 20, _imageView2.width, 20)];
    _titleLabel1.text = model1.title;
    _titleLabel1.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
    _titleLabel1.textAlignment = NSTextAlignmentCenter;
    _titleLabel1.textColor = [UIColor whiteColor];
    [_imageView1 addSubview:_titleLabel1];

    _titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, _imageView2.height - 20, _imageView2.width, 20)];
    _titleLabel2.text = model2.title;
    _titleLabel2.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
    _titleLabel2.textAlignment = NSTextAlignmentCenter;
    _titleLabel2.textColor = [UIColor whiteColor];
    [_imageView2 addSubview:_titleLabel2];

    _titleLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, _imageView1.height - 20, _imageView2.width, 20)];
    _titleLabel3.text = model3.title;
    _titleLabel3.textAlignment = NSTextAlignmentCenter;
    _titleLabel3.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
    _titleLabel3.textColor = [UIColor whiteColor];
    [_imageView3 addSubview:_titleLabel3];

//    _count = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //滑动视图切换始终处于第二张
    if ((scrollView.contentOffset.x) / kScreenWidth > 1.995) {
        
        scrollView.contentOffset = CGPointMake(kScreenWidth * 0.995, 0);
        _count++;
        if (_count > _dataArray.count - 1) {
            _count = 0;
        }
        [self exchangeData];
        
    }else if((scrollView.contentOffset.x / kScreenWidth) == 0){
        
        scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        _count--;
        if (_count < 0) {
            _count = _dataArray.count - 1;
        }
        [self exchangeData];
    }
    
}

- (void)exchangeData {
    //界面图片变换
    NewsModel *model1 = nil;
    NewsModel *model2 = _dataArray[_count];
    NewsModel *model3 = nil;
    if (_count == 0) {
        
        model1 = _dataArray[_dataArray.count -1];
        
    }else {
        model1 = _dataArray[_count - 1];
    }
        
    if (_count == _dataArray.count - 1) {
        
        model3 = _dataArray[0];
        
    }else {
        
        model3 = _dataArray[_count + 1];
    }
    
    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:model1.image]];
    [_imageView2 sd_setImageWithURL:[NSURL URLWithString:model2.image]];
    [_imageView3 sd_setImageWithURL:[NSURL URLWithString:model3.image]];
    
    _titleLabel1.text = model1.title;
    _titleLabel2.text = model2.title;
    _titleLabel3.text = model3.title;
}
@end
